package ac.github.os.internal.core.action.plugin

import ac.github.os.api.OriginSkillAPI
import ac.github.os.internal.core.action.Action
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.holder.plugin.CallSkillAction
import ac.github.os.internal.core.objective.EntityObjective
import ac.github.os.internal.core.objective.LocationObjective
import ac.github.os.internal.core.trigger.EntityTrigger
import ac.github.os.internal.core.trigger.LocationTrigger
import ac.github.os.internal.core.trigger.PlayerTrigger
import ac.github.os.internal.session.Cache
import ac.github.os.internal.session.Session
import org.bukkit.entity.Player
import taboolib.common.platform.Awake

@Awake
class CallAction : Action {
    override fun onExecute(session: Session, cache: Cache, config: StringConfig) {

        cache.objectives.forEach {
            config.get<String>("val", single = true)?.apply {
                when (it::class.java) {
                    EntityObjective::class.java -> {
                        it as EntityObjective
                        val livingEntity = it.entity
                        val entityTrigger =
                            if (livingEntity is Player) PlayerTrigger(livingEntity) else EntityTrigger(livingEntity)
                        OriginSkillAPI.cast(entityTrigger, this, CallSkillAction())
                    }
                    LocationObjective::class.java -> {
                        OriginSkillAPI.cast(LocationTrigger((it as LocationObjective).location), CallSkillAction())
                    }
                }
            }
        }

    }

    override val type: Action.Type
        get() = Action.Type.ENTITY
    override val namespace: String
        get() = "call"
}